#include <stdio.h>
#include <libspe.h>

//This global is for the SPE program code itself.  It will be created by
//the embedspu program.
extern spe_program_handle_t calculate_distance_handle;

//This struct is used for input/output with the SPE task
typedef struct {
	float speed;     //input parameter
	float num_hours; //input parameter
	float distance;  //output parameter
	float padding;   //pad the struct a multiple of 16 bytes
} program_data;

int main() {
	program_data pd __attribute__((aligned(16)));  //aligned for transfer

	//GATHER DATA TO SEND TO SPE
	printf("Enter the speed at which your car is travelling in miles/hr: ");
	scanf("%f", &pd.speed);
	printf("Enter the number of hours you have been driving at that speed: ");
	scanf("%f", &pd.num_hours);

	//USE THE SPE TO PROCESS THE DATA
	//Create SPE Task
	speid_t spe_id = spe_create_thread(0, &calculate_distance_handle, &pd, NULL,
	 -1, 0);
	//Check For Errors
	if(spe_id == 0) {
		fprintf(stderr, "Error creating SPE thread!\n");
		return 1;
	}
	//Wait For Completion
	spe_wait(spe_id, NULL, 0);

	//FORMAT THE RESULTS FOR DISPLAY
	printf("The distance travelled is %f miles.\n", pd.distance);
	return 0;
}

